/**
 * @author Linrui
 * @since 2011-04-29
 * 用户登录拦截器
 */
package com.qeado.util.interceptors;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@Controller
public class LoginValidationInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		/* If the user have not been logined , it must return Login_Fail and forward to login.jsp */
		if( ActionContext.getContext().getSession().get("login_user") == null ) {
			HttpServletResponse response = ServletActionContext.getResponse();
			String base = ServletActionContext.getRequest().getContextPath();
			PrintWriter out = response.getWriter();
		    out.println("<html>");  
		    out.println("<script type=\"text/javascript\">");  
		    out.println("window.open ('" + base + "/login','_top')");  
		    out.println("</script>");  
		    out.println("</html>");
			return null;
		} else {
			return invocation.invoke();
		}
	}

}
